package tq.com.tqteacher.activity.im;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import android.view.View;

import org.json.JSONObject;

import tq.com.extern.TQCallResult;
import tq.com.extern.TQIMClient;
import tq.com.tqim.net.TQImCallback;
import tq.com.tqim.net.TQImRequest;
import tq.com.extern.TQNetError;
import tq.com.tqteacher.R;
import tq.com.tqteacher.view.TQInputView;

public class QTImLogin extends AppCompatActivity {
    private TQInputView name;
    private TQInputView pwd;

    static private String TAG = "QTImLogin";

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_im_login);

        name = findViewById(R.id.name);
        pwd = findViewById(R.id.password);
        name.setText("abc1234");
        pwd.setText("123");

        findViewById(R.id.login).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                doLogin(name.getText(), pwd.getText());
            }
        });

        findViewById(R.id.register).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                doRegister(name.getText(), pwd.getText());
            }
        });
    }


    private void doLogin(String name, String pwd){
        TQIMClient.client().login(name, pwd, new TQCallResult() {
            @Override
            public void failed(TQNetError error) {
                Log.d(TAG, "failed: ");
            }

            @Override
            public void success() {

            }
        });
    }

    private void doRegister(String name, String pwd){
        TQImRequest.registerIm(name, pwd, new TQImCallback() {
            @Override
            public void failed(TQNetError error) {
                Log.d(TAG, error.message);
            }

            @Override
            public void succes(JSONObject response) {

            }
        });
    }
}
